package cn.rongdao.jrkabao.ui.loan;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.chanven.lib.cptr.PtrClassicDefaultHeader;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerViewHolder;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.main.MainBean;
import cn.rongdao.jrkabao.bean.main.ProductListBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.model.main.MainModel;
import cn.rongdao.jrkabao.ui.login.LoginActivity;
import cn.rongdao.jrkabao.ui.main.mainfragment.adapter.FragmentMainAdapter;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.LoginUtil;

/*
* Created by Wang on 17/11/9.
* 贷款列表界面
* */
public class LoanListActivity extends BaseAutoFinishActivity implements FRBaseRecyclerAdapter.OnItemClickListener<FRBaseRecyclerViewHolder<ProductListBean>>{
    @BindView(R.id.loanlist_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.loanlist_recyclerView)
    PtrRecyclerViewUIComponent recyclerViewUIComponent;

    private RecyclerAdapterWithHF mRecyclerAdapterWithHF;
    private FragmentMainAdapter mFragmentMainAdapter;
    private List<ProductListBean> mProductListBeanList ;
    public static void start(Context context){
        FRStartActivity.start(context, LoanListActivity.class);
    }


    @Override
    protected int layoutRes() {
        return R.layout.activity_loan_list;
    }

    @Override
    protected void initView() {
      mProductListBeanList = new ArrayList<>();
      initTitleBar();
      initRecyclerView();
      initListener();
      getData();
    }

    private void initListener() {
        recyclerViewUIComponent.delayRefresh(0);
        recyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                if (recyclerViewUIComponent.isAutoRefresh()) {
                    return;
                }
                getData();
            }
        });
    }
    /*初始化RecyclerView*/
    private void initRecyclerView() {
        mFragmentMainAdapter = new FragmentMainAdapter(this, this);
        mRecyclerAdapterWithHF = new RecyclerAdapterWithHF(mFragmentMainAdapter);
        recyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewUIComponent.setAdapter(mRecyclerAdapterWithHF);
        recyclerViewUIComponent.setBackground(R.color.layout_bg);
        recyclerViewUIComponent.setHeaderType(PtrClassicDefaultHeader.HEADER_TYPE_ANIM);

    }
    /*初始化Title*/
    private void initTitleBar() {
        //返回键
        titleBarUIComponent.initLeftBackBlack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //标题
        titleBarUIComponent.initTitleBlack("贷款列表");
        titleBarUIComponent.setBackgroundColor(Color.WHITE);
    }
    /*获取数据*/
    public void getData() {
        MainModel.getInstance().execute(new FRBaseObserver<MainBean>(this) {
            @Override
            protected void onBaseNext(final MainBean data) {
                if (null != data) {
                    Log.e("WANG","LoanListActivity.onBaseNext." +data);
                    parseData(data);

                }
                recyclerViewUIComponent.refreshComplete();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                recyclerViewUIComponent.refreshComplete();
            }
        });
    }

    private void parseData(MainBean mainBean) {
        mProductListBeanList.clear();
        mProductListBeanList.addAll(mainBean.getProduct_list());
        mFragmentMainAdapter.setDataList(mProductListBeanList, false);
        mFragmentMainAdapter.notifyDataSetChanged();
    }
    /*item的点击事件*/
    @Override
    public void onItemClick(FRBaseRecyclerViewHolder<ProductListBean> productListBeanFRBaseRecyclerViewHolder, int position) {
        ProductListBean productListBean = mProductListBeanList.get(position);
        String product_id = productListBean.getProduct_id();
        String cate_id = productListBean.getCate_id();
        if (notYetLoggedIn()) return;
        startLoanProduct(productListBean);
    }
    private void startLoanProduct(ProductListBean productListBean) {

        if (productListBean.getJump_type() == 1) {
            WebViewActivity.start(this, productListBean.getOrg_link());
            return;
        }
        if (startMultiPustListActivity(productListBean)) return;

        LoanCommitActivity.start(this, productListBean.getCate_id(), productListBean.getProduct_id(), productListBean.getOrg_name());
    }

    private boolean startMultiPustListActivity(ProductListBean productListBean) {
        if ("1".equals(productListBean.getIs_apply()) && "1".equals(productListBean.getIs_push_all())) {
            MultiPusuListActivity.start(this, productListBean.getCate_id(), MultiPusuListActivity.CLASSIFY_MULTI_PUSH, productListBean.getOrg_name());
            return true;
        }
        return false;
    }

    boolean notYetLoggedIn() {
        if (!LoginUtil.isLogin()) {
            startActivityForResult(new Intent(this, LoginActivity.class), InfoConstant.REQUEST_CODE_LOANFRAGMENT_LOGINACTIVITY);
            return true;
        }
        return false;
    }

}
